package com.mdd.front.service.impl;

import com.mdd.common.entity.user.User;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.utils.ArithUtil;
import com.mdd.common.utils.LsConfigUtil;
import com.mdd.front.service.points.IPointsService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

@Service
public class PointsServiceImpl implements IPointsService {


    @Resource
    private UserMapper userMapper;

    @Override
    public Map<String, Object> getSetting(Integer userId, BigDecimal orderPrice) {

        orderPrice = orderPrice != null ? orderPrice : new BigDecimal("0");

        orderPrice = orderPrice.compareTo(BigDecimal.ZERO) == -1 ? new BigDecimal("0") : orderPrice;

        User user = userMapper.selectById(userId);
        Assert.notNull(user, "当前用户不存在");

        BigDecimal userPoints = user.getUserPoints();
        userPoints = userPoints != null ? userPoints : new BigDecimal("0");

        String enable = LsConfigUtil.get("point_deduction", "enable", "1");
        String scaleStr = LsConfigUtil.get("point_deduction", "scale", "10");
        String lowerLimitStr = LsConfigUtil.get("point_deduction", "lower_limit", "10");
        String superiorLimitStr = LsConfigUtil.get("point_deduction", "superior_limit", "10");

        BigDecimal lowerLimit = new BigDecimal(lowerLimitStr);
        BigDecimal superiorLimit = new BigDecimal(superiorLimitStr);
        BigDecimal scale = new BigDecimal(scaleStr);
        BigDecimal usedPoints = new BigDecimal("0");

        Boolean isDeductible = false;

        BigDecimal deductibleAmount = new BigDecimal("0");

        if (userPoints.compareTo(BigDecimal.ZERO) == 1) {
            if (userPoints.compareTo(lowerLimit) == 1 || lowerLimit.compareTo(BigDecimal.ZERO) == 0) {
                isDeductible = true;

                if (superiorLimit.compareTo(BigDecimal.ZERO) == 0) {
                    deductibleAmount = userPoints.divide(scale,2, RoundingMode.HALF_UP);
                    usedPoints = userPoints;
                } else if (userPoints.compareTo(superiorLimit) == 1) {
                    usedPoints = superiorLimit;
                    deductibleAmount = superiorLimit.divide(scale,2, RoundingMode.HALF_UP);
                } else {
                    deductibleAmount = userPoints.divide(scale,2, RoundingMode.HALF_UP);
                    usedPoints = userPoints;
                }

                if (deductibleAmount.compareTo(orderPrice) == 1) {
                    deductibleAmount = orderPrice;
                    usedPoints = deductibleAmount.multiply(scale);
                }

                usedPoints = usedPoints.setScale(0, BigDecimal.ROUND_UP);
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("enable", enable);
        result.put("scale", scaleStr);
        result.put("lowerLimit", lowerLimitStr);
        result.put("superiorLimit", superiorLimitStr);
        result.put("isDeductible", isDeductible);
        result.put("deductibleAmount", deductibleAmount);
        result.put("userPoints", userPoints);
        result.put("usedPoints", usedPoints);

        return result;
    }
}
